import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import '../../base/Constant.dart';
// import '../../util/MySeparator.dart';
import 'dart:convert';
import 'package:dio/dio.dart';
import '../../http/Api.dart';
import '../../http/HttpUtil.dart';
import '../../util/SignUtil.dart';
import '../../util/AppSaveInfoUtil.dart';

class MyCouponSubPage extends StatefulWidget {
  int type;
  MyCouponSubPage({Key key, @required this.type}) : super(key: key);

  @override
  _MyCouponSubPageState createState() => _MyCouponSubPageState();
}

class _MyCouponSubPageState extends State<MyCouponSubPage> {
  EasyRefreshController _controller = new EasyRefreshController();
  String token;
  int page = 1;
  List coupons = [];
  @override
  Widget build(BuildContext context) {
    return Container(
      child: EasyRefresh(
        controller: _controller,
        enableControlFinishLoad: true,
        enableControlFinishRefresh: true,
        onLoad: () {
          page++;
        },
        onRefresh: () {
          page = 1;
        },
        child: ListView.builder(
          itemBuilder: (BuildContext context, int index) {
            return renderRow(context, index);
          },
          itemCount: coupons.length,
        ),
      ),
    );
  }

  Widget renderRow(BuildContext context, int index) {
    return Container(
      margin: EdgeInsets.fromLTRB(
          ScreenUtil.getInstance().setHeight(DP_20),
          ScreenUtil.getInstance().setHeight(DP_20),
          ScreenUtil.getInstance().setHeight(DP_20),
          0),
      // padding: EdgeInsets.all(ScreenUtil.getInstance().setHeight(DP_20)),
      decoration: BoxDecoration(
          color: white,
          borderRadius:
              BorderRadius.circular(ScreenUtil.getInstance().setHeight(DP_10))),
      child: Stack(
        children: <Widget>[
          Container(
            height: kMargin140*1.4,
            // padding: EdgeInsets.all(ScreenUtil.getInstance().setHeight(DP_20)),
            child: Row(

              children: <Widget>[
                
                Container(
                  width: kMargin140*1.4,
                  decoration:widget.type == 0
                                  ? BoxDecoration(
                    image: DecorationImage(
                      image: AssetImage("images/quan_bg_miane.png"),
                      fit: BoxFit.fill,
                    ),
                  ):null,
                  // quan_bg_miane
                  child: Column(
                    children: <Widget>[
                      Container(
                        height: ScreenUtil.getInstance().setWidth(DP_40),
                      ),
                      Text(MONEY + coupons[index]['money'],
                          style: TextStyle(
                              fontSize: ScreenUtil.getInstance()
                                  .setSp(BIG_FONT_SIZE * 1.2),
                              // fontWeight: FontWeight.bold,
                              color: widget.type == 0
                                  ? white
                                  : Colors.black45)),
                      Container(
                        height: ScreenUtil.getInstance().setWidth(DP_8),
                      ),
                      Text(
                          coupons[index]['condition_money'] == '0'
                              ? '无门槛使用'
                              : '满' + coupons[index]['condition_money'] + '元可用',
                          style: TextStyle(
                              fontSize: ScreenUtil.getInstance()
                                  .setSp(MINI_FONT_SIZE),
                              color: white)),
                    ],
                  ),
                ),
                Container(
                  width: ScreenUtil.getInstance().setWidth(DP_60),
                ),
                Expanded(
                    child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Container(
                        height: ScreenUtil.getInstance().setWidth(DP_30),
                      ),
                    Text(coupons[index]['name'],
                        style: TextStyle(
                            fontSize:
                                ScreenUtil.getInstance().setSp(BIG_FONT_SIZE),
                            color: widget.type == 0 ? black : Colors.black45)),
                    Container(
                      height: ScreenUtil.getInstance().setWidth(DP_15),
                    ),
                    Text(
                        '有效期:' +
                            coupons[index]['use_start_time'] +
                            '至' +
                            coupons[index]['use_end_time'],
                        style: TextStyle(
                            fontSize:
                                ScreenUtil.getInstance().setSp(MINI_FONT_SIZE),
                            color:
                                widget.type == 0 ? black_99 : Colors.black45)),
                  ],
                )),
              ],
            ),
          ),
          widget.type == 2
              ? Positioned(
                  child: Container(
                    padding: EdgeInsets.fromLTRB(
                        ScreenUtil.getInstance().setHeight(DP_20),
                        4,
                        ScreenUtil.getInstance().setHeight(DP_20),
                        4),
                    decoration: BoxDecoration(
                      color: Colors.orange[300],
                      borderRadius: BorderRadius.only(
                          bottomLeft: Radius.circular(
                              ScreenUtil.getInstance().setHeight(DP_10)),
                          topRight: Radius.circular(
                              ScreenUtil.getInstance().setHeight(DP_10))),
                    ),
                    child: Text('即将过期',
                        style: TextStyle(
                            color: white,
                            fontSize: ScreenUtil.getInstance()
                                .setSp(MINI_FONT_SIZE))),
                  ),
                  right: 0,
                )
              : Positioned(
                  right: ScreenUtil.getInstance().setHeight(DP_20),
                  top: 10,
                  bottom: 10,
                  child: widget.type == 1
                      ? Image.asset(
                          "images/coupon_used.png",
                          scale: 1.2,
                        )
                      : (widget.type == 2?Image.asset(
                          "images/coupon_expried.png",
                          scale: 1.2,
                        ):Container())
                        )
        ],
      ),
    );
  }

  @override
  void initState() {
    getString(TOKEN).then((value) {
      token = value;
      getList();
    });

    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  Future getList() async {
    //优惠券列表
    Map map = new Map.from({
      "sign_type": createSignType(Api.URL_COUPON_USERLIST),
      'token': token,
      'p': page.toString(),
      'status': (widget.type + 1).toString()
    });
    Response<String> response =
        await HttpUtil().post(Api.URL_COUPON_USERLIST, data: getSign(map));
    Map userMap = json.decode(response.data);
    print(userMap);
    if (userMap['code'] == 1 && mounted) {
      setState(() {
        coupons.clear();
        _controller.finishRefresh();
        coupons = userMap['data']['list'];
      });
    }
  }
}
